/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "DayButton.h"

namespace FaceCat{
    Long DayButton::getPaintingBackColor(){
        if (m_selected){
            return FCColor_Pushed;
        }
        else{
            if (m_inThisMonth){
                return FCColor_Hovered;
            }
            else{
                return FCColor_Back;
            }
        }
    }
    
    Long DayButton::getPaintingBorderColor(){
        return FCColor_Border;
    }
    
    Long DayButton::getPaintingTextColor(){
        return FCColor_Text;
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    DayButton::DayButton(FCCalendar *calendar){
        m_bounds.left = 0;
        m_bounds.top = 0;
        m_bounds.right = 0;
        m_bounds.bottom = 0;
        m_calendar = calendar;
        m_day = 0;
        m_inThisMonth = false;
        m_selected = false;
        m_visible = true;
    }
    
    DayButton::~DayButton(){
        m_calendar = 0;
    }
    
    FCRect DayButton::getBounds(){
        return m_bounds;
    }
    
    void DayButton::setBounds(const FCRect& value){
        m_bounds = value;
    }
    
    FCCalendar* DayButton::getCalendar(){
        return m_calendar;
    }
    
    void DayButton::setCalendar(FCCalendar *value){
        m_calendar = value;
    }
    
    CDay* DayButton::getDay(){
        return m_day;
    }
    
    void DayButton::setDay(CDay *value){
        m_day = value;
    }
    
    bool DayButton::inThisMonth(){
        return m_inThisMonth;
    }
    
    void DayButton::setThisMonth(bool value){
        m_inThisMonth = value;
    }
    
    bool DayButton::isSelected(){
        return m_selected;
    }
    
    void DayButton::setSelected(bool value){
        m_selected = value;
    }
    
    bool DayButton::isVisible(){
        return m_visible;
    }
    
    void DayButton::setVisible(bool value){
        m_visible = value;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void DayButton::onClick(FCTouchInfo touchInfo){
        if(m_calendar && m_day){
            m_calendar->setSelectedDay(m_day);
        }
    }
    
    void DayButton::onPaintBackground(FCPaint *paint, const FCRect& clipRect){
        Long backColor = getPaintingBackColor();
        paint->fillRect(backColor, m_bounds);
    }
    
    void DayButton::onPaintBorder(FCPaint *paint, const FCRect& clipRect){
        Long borderColor = getPaintingBorderColor();
        paint->drawLine(borderColor, 1, 0, m_bounds.left, m_bounds.bottom - 1, m_bounds.right - 1, m_bounds.bottom - 1);
        paint->drawLine(borderColor, 1, 0, m_bounds.right - 1, m_bounds.top, m_bounds.right - 1, m_bounds.bottom - 1);
    }
    
    void DayButton::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
        if(m_day){
            int width = m_bounds.right - m_bounds.left;
            int height = m_bounds.bottom - m_bounds.top;
            int day = m_day->getDay();
            wchar_t tDay[10] ={0};
            swprintf(tDay, 9, L"%d", day);
            FCFont *font = m_calendar->getFont();
            FCSize textSize = paint->textSize(tDay, font, -1);
            int tLeft = m_bounds.left + (width - textSize.cx) / 2;
            int tTop = m_bounds.top + (height - textSize.cy) / 2;
            FCRect textRect ={tLeft, tTop, tLeft + textSize.cx, tTop + textSize.cy};
            paint->drawText(tDay, getPaintingTextColor(), font, textRect, -1);
        }
    }
}
